package com.enjoy.xbase.qk.cookie;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import okhttp3.internal.Util;


public class SPCookie implements Serializable {

    public String name;
    public String value;
    public long expiresAt;
    public String domain;
    public String path;
    public boolean secure;
    public boolean httpOnly;
    public boolean persistent; // True if 'expires' or 'max-age' is present.
    public boolean hostOnly; // True un


    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(name + "=" + value + ";");

        if (persistent) {
            if (expiresAt == Long.MIN_VALUE) {
                result.append(" max-age=0;");
            } else {
                DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                dateFormat.setLenient(false);
                dateFormat.setTimeZone(Util.UTC);
                result.append(" expires=" + dateFormat.format(new Date(expiresAt)) + ";");
            }
        }

        if (!hostOnly) {
            result.append(" domain=" + domain + ";");
        }

        result.append("path=" + path + ";");

        if (secure) {
            result.append(" secure;");
        }

        if (httpOnly) {
            result.append(" httponly;");
        }

        return result.substring(0, result.length() - 1);
    }

}
